
(function(Global /*, context */) {


var isTalentTooltipVisible = false;
var rowsText = {};
var rowsDesc={}
var statBranchColumnELE=null
var talentDescriptionsELE=null
var talentPanel=null




function HideLevels() {
    talentPanel.FindChildTraverse('LevelColumn').style.visibility="collapse"
}





function ChooseBranch(row,col)
{
    Game.EmitSound("BUTTON_CLICK_MAJOR");
    GameEvents.SendCustomGameEventToServer("talent_manager_choose_talent", { "playerId": Game.GetLocalPlayerID(),"row": row,"col":col});

}


function SetAddTalentButtonVisible(visible)
{
	var x = $.GetContextPanel().GetParent().GetParent().GetParent()
	x = x.FindChildTraverse('HUDElements')
	x = x.FindChildTraverse('lower_hud')
	x = x.FindChildTraverse('center_with_stats')
	x = x.FindChildTraverse('center_block') 
	x = x.FindChildTraverse('level_stats_frame') 
	x.style.visibility = visible ? "visible;" : "collapse;";
}

function SetTalentTooltipVisible(visible)
{
	if(!Entities.IsControllableByPlayer(Players.GetLocalPlayerPortraitUnit(), Game.GetLocalPlayerID())) return;
	if(visible){
        talentPanel.style.visibility="visible"
	}
	else {
        talentPanel.style.visibility="collapse"
	}

    isTalentTooltipVisible=visible;
}

function SetActivedLabel(lb,enabled) {

}

function Init()
{
     talentDescriptionsELE=Global.Panel.dotaHud.FindChildTraverse("HUDElements").FindChildTraverse("StatBranchDrawer")
		 .FindChildTraverse("DOTAStatBranch").FindChildTraverse("TalentDescriptions")
    talentPanel=Global.Panel.dotaHud.FindChildTraverse("HUDElements").FindChildTraverse("StatBranchDrawer")
        .FindChildTraverse("DOTAStatBranch");

    talentDescriptionsELE.style.visibility="collapse"


    function OnLabelHover(label)
    {
        if(label.GetParent().GetParent().BHasClass("BranchActiveExt"))
        {
            label.style.textShadow = "0px 0px 3px 3.7 #EC780E24";
            label.style.color = "#E7D291";
        }
    }

    function OnLabelDehover(label)
    {
        label.style.textShadow = null;
        label.style.color = null;
    }

    function OnLabelClicked(row,col)
    {
        ChooseBranch(row,col)
    }

    function InitOption(i) {
        if(rowsText[i]!==undefined){
            return
        }
        var upgradeOption= Global.Panel.dotaHud.FindChildTraverse("HUDElements").FindChildTraverse("StatBranchDrawer")
            .FindChildTraverse("DOTAStatBranch").FindChildTraverse("StatBranchColumn").FindChildTraverse('UpgradeOption'+i);
        upgradeOption.selected = "none";
        rowsText[i]=upgradeOption;

        var lBtn = upgradeOption.FindChildTraverse('Upgrade' + (i * 2));
        var rBtn = upgradeOption.FindChildTraverse('Upgrade' + (i * 2 - 1));


        lBtn.enabled = false;
        rBtn.enabled = false;

        lBtn.style.visibility = 'collapse'
        rBtn.style.visibility = 'collapse'

        var newLeftBtn= $.CreatePanel('Button', upgradeOption, 'CleanUpgrade'+ (i*2));
        var newRightBtn= $.CreatePanel('Button', upgradeOption, 'CleanUpgrade'+ (i*2-1));
        newLeftBtn.hittest = true;
        newLeftBtn.enabled = false;
        newLeftBtn.SetHasClass('BranchChoice', true);
        newLeftBtn.SetHasClass('LeftBranch', true);

        newRightBtn.hittest = true;
        newRightBtn.enabled = false;
        newRightBtn.SetHasClass('BranchChoice', true);
        newRightBtn.SetHasClass('RightBranch', true);

        var newLeftLB = $.CreatePanel('Label', newLeftBtn, 'TextLabel');
        var newRightLB = $.CreatePanel('Label', newRightBtn, 'TextLabel');
        newRightLB.html=true;
        newLeftLB.html=true;
        newLeftLB.text=""
        newRightLB.text=""

        newLeftLB.SetHasClass('StatBonusLabel', true);
        newRightLB.SetHasClass('StatBonusLabel', true);

        newRightBtn.label=newRightLB
        newLeftBtn.label=newLeftLB

        newLeftBtn.SetPanelEvent('onmouseover', function(){OnLabelHover(newLeftLB)});
        newLeftBtn.SetPanelEvent('onmouseout', function(){OnLabelDehover(newLeftLB)});
        newLeftBtn.SetPanelEvent('onactivate', function(){OnLabelClicked(i,2)});
        newRightBtn.SetPanelEvent('onmouseover', function(){OnLabelHover(newRightLB)});
        newRightBtn.SetPanelEvent('onmouseout', function(){OnLabelDehover(newRightLB)});
        newRightBtn.SetPanelEvent('onactivate', function(){OnLabelClicked(i,1)});


        // rowsDesc[i*2] = talentDescriptionsELE.FindChildTraverse('Upgrade' + (i*2) +"Container");
        // rowsDesc[i*2-1] = talentDescriptionsELE.FindChildTraverse('Upgrade' + (i*2-1) +"Container");
    }

    for(var i=1;i<=4;i++){
    	InitOption(i)
    }

    var x =  Global.Panel.dotaHud.FindChildTraverse("HUDElements").FindChildTraverse("lower_hud")
        .FindChildTraverse("center_with_stats").FindChildTraverse('center_block')
    x.FindChildTraverse('StatBranch').SetPanelEvent('onactivate', function(){});


    x.FindChildTraverse('StatBranchChannel').SetPanelEvent('onmouseover', function(){
        SetTalentTooltipVisible(true);
	});
    x.FindChildTraverse('StatBranchChannel').SetPanelEvent('onmouseout', function(){
        SetTalentTooltipVisible(false);
    });

    x = x.FindChildTraverse('level_stats_frame').FindChildTraverse("LevelUpTab");

    x.SetPanelEvent("onactivate", function()
    {
        SetTalentTooltipVisible(!isTalentTooltipVisible);
    });


    HideLevels();
}

function RefreshTalent(data) {
	$.Msg("RefreshTalent	",data)


	var showLevelBtn=false
    for(var i in data)
    {
        var rowData = data[i];

    	var leftBtn=rowsText[i].FindChildTraverse('CleanUpgrade' + (i * 2));
        var rightBtn=rowsText[i].FindChildTraverse('CleanUpgrade' + (i * 2-1));

        var rightCanLearn=(rowData[1]&&(rowData[1].canLearn==true))
        var leftCanLearn=(rowData[2]&&(rowData[2].canLearn==true))
		var rightLearned=(rowData[1]&&(rowData[1].learned==true))
        var leftLearned=(rowData[2]&&(rowData[2].learned==true))

		if(rightCanLearn||leftCanLearn&&(!leftLearned&&!rightLearned)){
            showLevelBtn=true
            rowsText[i].SetHasClass('BranchActiveExt', showLevelBtn);
            rowsText[i].style.backgroundImage = "url('s2r://panorama/images/hud/reborn/statbranch_select_bg_psd.vtex');";
            rowsText[i].style.backgroundSize = " 98% 88%;";
            rowsText[i].style.backgroundPosition = "center;";
            rowsText[i].style.backgroundRepeat = "no-repeat;";
            leftBtn.enabled =leftCanLearn;
            rightBtn.enabled =rightCanLearn;
            leftBtn.SetHasClass("Disabled",!leftCanLearn)
            rightBtn.SetHasClass("Disabled",!rightCanLearn)
		}
		else{
            rowsText[i].style.backgroundImage = "none";
            leftBtn.enabled =!leftLearned;
            rightBtn.enabled =!rightLearned;
            leftBtn.SetHasClass("Disabled",true)
            rightBtn.SetHasClass("Disabled",true)
   		 }


        leftBtn.SetHasClass("Activated",leftLearned)
        rightBtn.SetHasClass("Activated",rightLearned)
   		 leftBtn.SetHasClass("BranchChosen",leftLearned)
        rightBtn.SetHasClass("BranchChosen",rightLearned)



        leftBtn.label.SetDialogVariable("value", rowData[2].value);
        rightBtn.label.SetDialogVariable("value", rowData[1].value);
        leftBtn.label.text = rowData[2].name?$.Localize("#DOTA_Tooltip_ability_" + rowData[2].name,leftBtn.label):""
        rightBtn.label.text = rowData[1].name?$.Localize("#DOTA_Tooltip_ability_" + rowData[1].name,rightBtn.label):""


    }

	if(showLevelBtn===false){
        SetTalentTooltipVisible(showLevelBtn);
	}
    SetAddTalentButtonVisible(showLevelBtn);
}

function OnTalentChanged(table, key, data) {
    if ( key == Game.GetLocalPlayerID() ) {
        RefreshTalent(data)
    }
}

Init();
CustomNetTables.SubscribeNetTableListener("talent_manager", OnTalentChanged);


})(GameUI.CustomUIConfig(), this);